<template>
  <el-dialog width="60%" :before-close="dataFormCancle" :title="title" :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="会员" prop="memberId">
            <el-select ref="villageRef" filterable v-model="dataForm.memberId" placeholder="请选择" style="width: 100%">
              <el-option v-for="item in memberList" :key="item.id" :label="item.name" :value="item.id">
                <span style="float: left">{{ item.name }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.mobile }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="姓名" prop="name">
            <el-input v-model="dataForm.name" placeholder="姓名"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="性别" prop="sex">
            <el-select v-model="dataForm.sex" placeholder="性别" clearable style="width: 100%">
              <el-option v-for="dict in dict.type.sys_user_sex" :key="Number(dict.value)" :label="dict.label"
                :value="Number(dict.value)" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="年龄" prop="age">
            <el-input type="number" v-model="dataForm.age" placeholder="年龄"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" prop="status">
            <el-select v-model="dataForm.status" placeholder="状态" clearable style="width: 100%">
              <el-option v-for="dict in dict.type.sys_card_status" :key="Number(dict.value)" :label="dict.label"
                :value="Number(dict.value)" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="开始日期" prop="beginDate">
            <el-date-picker disabled v-model="dataForm.beginDate" style="width: 100%" type="date"
              placeholder="开始日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="截止日期" prop="endDate">
            <el-date-picker disabled v-model="dataForm.endDate" style="width: 100%" type="date"
              placeholder="截止日期"></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="次数" prop="count">
            <el-input disabled v-model="dataForm.count" placeholder="次数"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <div @click="takePhone(dataForm)">
            <el-form-item label="头像" prop="url">
              <MemberImgVue :src="dataForm.url" :imgWidth="'160px'" />
            </el-form-item>
          </div>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" rows="6" v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <MyCamera ref="myCamera" @unpdateAvatar="unpdateAvatar('1',$event)"></MyCamera>
    <span slot="footer" class="dialog-footer">
      <el-button @click="dataFormCancle">取消</el-button>
      <el-button :loading="loading" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {
    addOrUpdate
  } from "@/api/business/cardinfo";
  import {
    page as getMembers
  } from '@/api/business/memberinfo'
  import MemberImgVue from "../MemberInfo/memberImg.vue";
  import MyCamera from '@/components/Camera/index.vue'
  export default {
    dicts: ['sys_user_sex', 'sys_card_status'],
    components: {
      MyCamera,
      MemberImgVue
    },
    data() {
      return {
        loading: false,
        title: '',
        visible: false,
        memberList: [],
        dataForm: {
          id: 0,
          name: '',
          sex: '',
          age: '',
          url: '',
          beginDate: '',
          endDate: '',
          createTime: '',
          status: '',
          count: '',
          remark: '',
          num: '',
          memberId: ''
        },
        dataRule: {
          name: [{
            required: true,
            message: '姓名不能为空',
            trigger: 'blur'
          }],
          sex: [{
            required: true,
            message: '性别不能为空',
            trigger: 'blur'
          }],
          age: [{
            required: true,
            message: '年龄不能为空',
            trigger: 'blur'
          }],
          url: [{
            required: true,
            message: '头像不能为空',
            trigger: 'blur'
          }],
          beginDate: [{
            required: true,
            message: '开始日期不能为空',
            trigger: 'blur'
          }],
          endDate: [{
            required: true,
            message: '结束日期不能为空',
            trigger: 'blur'
          }],
          createTime: [{
            required: true,
            message: '创建日期不能为空',
            trigger: 'blur'
          }],
          count: [{
            required: true,
            message: '次数不能为空',
            trigger: 'blur'
          }],
          remark: [{
            required: false,
            message: '备注不能为空',
            trigger: 'blur'
          }],
          num: [{
            required: true,
            message: '编号不能为空',
            trigger: 'blur'
          }],
          memberId: [{
            required: true,
            message: '会员ID不能为空',
            trigger: 'blur'
          }]
        }
      }
    },
    created() {
      getMembers({
        pageSize: 100000,
        pageNum: 1,
        search: {}
      }).then(res => {
        this.memberList = res.data.list
      })
    },
    methods: {
      takePhone(item) {
        this.$nextTick(() => {
          this.$refs.myCamera.init()
        })
      },
      unpdateAvatar(val, e) {
        console.log(e, '00000');
        this.dataForm.url = e
      },
      init(record) {
        this.visible = true
        this.dataForm = record
        if (this.dataForm && this.dataForm.id) {
          this.title = '年卡信息' + '修改'
        } else {
          this.title = '年卡信息' + "新增"
        }
      },
      //关闭表单
      dataFormCancle() {
        this.visible = false
        this.loading = false
        this.$emit('refreshDataList')
      },
      // 表单提交
      dataFormSubmit() {
        console.log(this.dataForm, '00----');
        if (!this.dataForm.url) {
          this.$message({
            message: '请上传年卡头像！',
            type: 'warning'
          });
          return
        }
        this.loading = true;
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            addOrUpdate(this.dataForm).then(res => {
              this.visible = false
              this.$modal.msgSuccess(this.title + "成功");
              this.$emit('refreshDataList')
            }).catch(res => {
              let _this = this
              setTimeout(() => {
                _this.loading = false
              }, 2000)
            })
          } else {
            this.loading = false
          }
        })
      }
    }
  }
</script>
